<script setup lang="ts">
  import { ref } from "vue";
  import Dialog from "@/components/sql/Dialog.vue";
  import { Filter } from "@/components/SideBar";
  import { arr2tree } from "@/util";
  const fd: gt.components.SideBar.Filter[] = [
    { value: "新增商品类型", type: "button", opt: { onClick() { click(0); }, type: "primary", class: "mac-ico-add-round", disabled: !api.me.per("商品类型创建") } },
  ];

  const
    page = ref<Tree<gt.sql.goodsType>>([]),
    entity = ref<InstanceType<typeof Dialog>>(),
    main = ref<HTMLDivElement>(),
    filterData = ref<any>(fd),
    tip = ref([
      { label: "添加商品类型", value: 0, cls: "mac-ico-add-round", per: api.me.per("商品类型创建") },
      { label: "编辑商品类型", value: 1, cls: "mac-ico-edit", per: api.me.per("商品类型修改") },
      { label: "查看商品类型", value: 2, cls: "mac-ico-search", per: true },
      { label: "删除商品类型", value: 3, cls: "mac-ico-delete", per: api.me.per("商品类型删除") },] as const);
  async function init() {
    const loading = ElLoading.service({ text: "正在加载,请稍后...", target: main.value });
    page.value = arr2tree(await gd.goodsType.read(), "id", "pid", 1);
    loading.close();
  }
  init();
  async function click(type: gt.curd, p?: gt.sql.goodsType) {
    if (type == 0 && p) {
      p = Object.assign(format.def.goodsType, { pid: p.id, permission: p.permission });
    }
    if (!p) {
      p = format.def.goodsType;
    }
    entity.value?.open("goodsType", type, p, "商品类型");
  }
  function confirm() {
    init();
  }
</script>
<template>
  <div class="fg1 y oh goodsType" ref="main">
    <Filter :data="filterData" isRow />
    <el-table stripe highlight-current-row :data="page" class="wf fg1" type="expand" row-key="id">
      <el-table-column prop="id" label="id" width="100" show-overflow-tooltip />
      <el-table-column prop="_createTime" label="创建时间" width="180" show-overflow-tooltip />
      <el-table-column prop="_goodsType.name" label="上级分组" show-overflow-tooltip />
      <el-table-column prop="_user.name" label="创建用户" show-overflow-tooltip />
      <el-table-column prop="name" label="名称" show-overflow-tooltip />
      <el-table-column label="操作" width="120">
        <template #default="{ row }">
          <div class="buttons x zb">
            <template v-for="a in tip">
              <el-tooltip v-if="a.per" :content="a.value ? a.label : '添加同级商品类型'">
                <div :class="a.cls" @click.stop="click(a.value, row)" />
              </el-tooltip>
            </template>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-pagination background layout="prev, pager, next, jumper, ->, sizes, total"
      :page-sizes="[10, 20, 30, 40, 50, 100]" v-model:page-size="page.size" :total="page.total"
      v-model:currentPage="page.current" @size-change="init" @current-change="init" /> -->
    <Dialog @confirm="confirm" ref="entity" />
  </div>
</template>
<style lang="scss" scoped>
  .goodsType {
    position: relative;

    .buttons {
      >div {
        cursor: pointer;
        padding: 0 0.4em;
        font-weight: 600;
        &:hover {
          border: 1px solid #a5a5a5;
          box-sizing: border-box;
          border-radius: 0.5em;
          box-shadow: 0 0 10px #000;
        }
      }
      >.mac-ico-add-round {
        color: var(--color-success);
      }
      >.mac-ico-edit {
        color: var(--color-warning);
      }
      >.mac-ico-search {
        color: var(--color-primary);
      }
      >.mac-ico-delete {
        color: var(--color-error);
      }
    }
  }
</style>